﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class PhieuGiaCong_DAO
    {
        private static PhieuGiaCong_DAO m_instance = null;

        public static PhieuGiaCong_DAO Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new PhieuGiaCong_DAO();
                return m_instance;
            }
        }

        /// <summary>
        ///   lay ma phieu gia cong mới nhất
        /// </summary>
        /// 
        public DataTable LayMaPhieuGiaCong()
        {
            DataTable result = new DataTable();
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("GET_MAPHIEUGIACONG_MAX", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(result);
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        ///   them phieu gia cong moi
        ///  
        /// </summary>
        /// 
        public bool ThemPhieuGiaCong(string _id, DateTime _ngayGC, DateTime _NgayNhanhHang, DateTime _ngayThanhToan, long _tongTien, string _idTho)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("ADD_PHIEUGIACONG", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@MaPhieuGC", SqlDbType.NVarChar, 10);
                cmd.Parameters["@MaPhieuGC"].Value = _id;

                cmd.Parameters.Add("@NgayGiaCong", SqlDbType.SmallDateTime);
                cmd.Parameters["@NgayGiaCong"].Value = _ngayGC.ToShortDateString();

                cmd.Parameters.Add("@NgayNhanHang", SqlDbType.SmallDateTime);
                cmd.Parameters["@NgayNhanHang"].Value = _NgayNhanhHang.ToShortDateString();

                cmd.Parameters.Add("@NgayThanhToan", SqlDbType.SmallDateTime);
                cmd.Parameters["@NgayThanhToan"].Value = _ngayThanhToan.ToShortDateString();

                cmd.Parameters.Add("@TongTien", SqlDbType.Money);
                cmd.Parameters["@TongTien"].Value = _tongTien;

                cmd.Parameters.Add("@MaTho", SqlDbType.NVarChar, 10);
                cmd.Parameters["@MaTho"].Value = _idTho;

                cmd.ExecuteNonQuery();

                return true;
            }
            catch (System.Exception ex)
            {
                return false;

            }
        }

        
    }
}
